﻿package lz.controls
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 * 单选按钮
	 * @author lxh
	 * 
	 */	
	public class RadioButton extends BaseButton
	{
		/**
		 *标签文本 
		 */		
		private var __labelTxt:TextField;
		/**
		 *选择mc 
		 */		
		private var __selectedMc:Sprite;
		/**
		 *组件皮肤 
		 */		
		private var __skin:MovieClip;
		/**
		 *标签内容 
		 */		
		private var _label:String;
		/**
		 *对齐方式 
		 */		
		private var _labelPlacement:String;
		/**
		 * 单选按钮组
		 */		
		private var _group:RadioButtonGroup;
		/**
		 *数据 
		 */		
		private var _value:Object;
		/**
		 *默认组 
		 */		
		protected var defaultGroupName:String = "RadioButtonGroup";
		/**
		 *间隔x 
		 */		
		private var grapX:Number=4;
		/**
		 *间隔y 
		 */		
		private var grapY:Number=4;
		/**
		 *皮肤字符串描述 
		 */		
		private var _skinClass :String;
		
		public function RadioButton()
		{
			super();
		}
		/**
		 * configUI
		 * 
		 */		
		override protected function configUI():void
		{
			skin = "SkinCheckBox";
		}
		/**
		 * 初始化
		 * 
		 */		
		private function initUI():void
		{
			__skin.gotoAndStop("normal");
			autoRepeat=false;
			selected=false;
			label="label";
			labelPlacement="right";
			__labelTxt.wordWrap=true;
			addListener();
			drawLayout();
			groupName = defaultGroupName;
		}
		/**
		 * 添加事件
		 * 
		 */		
		private function addListener():void
		{
			this.addEventListener(MouseEvent.ROLL_OVER,mouseHandle);
			this.addEventListener(MouseEvent.ROLL_OUT,mouseHandle);
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseHandle);
			this.addEventListener(MouseEvent.CLICK,mouseHandle);
			function mouseHandle(e:MouseEvent):void
			{
				switch(e.type)
				{
					case MouseEvent.ROLL_OVER:
						__skin.gotoAndStop("rollover");
						break;
					case MouseEvent.ROLL_OUT:
						__skin.gotoAndStop("normal");
						break;
					case MouseEvent.MOUSE_DOWN:
						__skin.gotoAndStop("down");
						break;
					case MouseEvent.CLICK:
						selected=true;
						if (_group != null) { _group.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true));}
						__skin.gotoAndStop("rollover");
				}
			}
		}
		/**
		 * 绘制背景
		 * 
		 */		
		override protected function drawBackground():void
		{
			
		}
		/**
		 * 
		 * 绘制布局
		 */		
		override protected function drawLayout():void
		{
			__skin.x=0;
			__skin.y=0;
			
			__selectedMc.x = 0;
			__selectedMc.y = 0;
			
			__labelTxt.width = this.width - __skin.width;
			if(_labelPlacement=="center")
			{
				__labelTxt.x=grapX;
				__labelTxt.width=__skin.width-2*grapX
				__labelTxt.autoSize="center";	
			}
			else if(_labelPlacement == "left")
			{
				
				__labelTxt.autoSize = "right";
				__labelTxt.x=-__labelTxt.width-grapX;
			}
			else if(_labelPlacement == "right")
			{
				__labelTxt.x=__skin.width+grapX;
				__labelTxt.autoSize="left";
			}
			__skin.y=(__labelTxt.height-__skin.height)>>1
			__selectedMc.y=(__labelTxt.height-__skin.height)>>1;
		}
		/**
		 *获取文本对象 
		 * @return 
		 * 
		 */		
		public function get labelTxt():TextField
		{
			return __labelTxt;
		}
		/**
		 *设置 组件皮肤 
		 * @param value
		 */		
		public function set skin(value:String):void
		{
			//trace("skin:",value);
			DisplayObjectUtil.removeAllChildren(this);
			_skinClass=value;
			_symbolSkin = Reflection.createInstance(_skinClass);
			this.addChild(_symbolSkin);
			
			__labelTxt		= _symbolSkin.__labelTxt;
			__selectedMc	= _symbolSkin.__selectedMc;
			__skin			= _symbolSkin.__skin;
			///
			initUI();
		}
		/**
		 *设置获取标签 
		 * @param value
		 * 
		 */		
		public function set label(value:String):void
		{
			_label=value;
			__labelTxt.htmlText=_label;
			drawLayout();
		}
		public function get label():String
		{
			return _label;
		}
		/**
		 *获取设置标签对齐方式 
		 * @param value
		 * 
		 */		
		public function set labelPlacement(value:String):void
		{
			_labelPlacement=value;
			drawLayout();
		}
		public function get labelPlacement():String
		{
			return _labelPlacement;
		}
		/**
		 *设置获取选择状态 
		 * @param value
		 * 
		 */		
		override public function set selected(value:Boolean):void
		{
			if (_group != null && value==true) { _group.selection = this; }
			_selected = value; 
			
			if(_selected)
			{
				__selectedMc.visible=true;
			}
			else
			{
				__selectedMc.visible=false;
			}
		}
		override public function get selected():Boolean
		{
			return _selected;
		}
		/**
		 *获取设置组 
		 * @param value
		 * 
		 */		
		public function set group(value:RadioButtonGroup):void
		{
			_group = value;
			groupName = value.name;
		}
		public function get group():RadioButtonGroup
		{
			return _group;
		}
		/**
		 *获取设置组名称 
		 * @return 
		 * 
		 */		
		public function get groupName():String
		{
			return (_group == null) ? null : _group.name;
		}
		public function set groupName(group:String):void 
		{
			if (_group != null) 
			{
				_group.removeRadioButton(this);
				_group.removeEventListener(Event.CHANGE,handleChange);
			}
			_group = (group == null) ? null : RadioButtonGroup.getGroup(group);
			if (_group != null)
			{
				_group.addRadioButton(this);
				_group.addEventListener(Event.CHANGE,handleChange,false,0,true);
			}
			
		}
		/**
		 *捕获变化 
		 * @param event
		 * 
		 */		
		protected function handleChange(event:Event):void 
		{
			selected = (_group.selection == this);
			dispatchEvent(new Event(Event.CHANGE, true));
		}
		/**
		 *获取设置数据 
		 * @param val
		 * 
		 */		
		public function set value(val:Object):void
		{
			_value=val;
		}
		public function get value():Object
		{
			return _value;
		}
		/**
		 *获取组件宽度 
		 * @return 
		 * 
		 */		
		public function get contentWidth():Number
		{
			return __skin.width+grapX+__labelTxt.width;
		}
		/**
		 *获取组件高度 
		 * @return 
		 * 
		 */		
		public function get contentHeight():Number
		{
			return __labelTxt.height;
		}
	}
}